package peer.p2pnet.message;

import java.net.DatagramPacket;

import peer.Message;
import peer.tools.ByteShortSwitch;

public class AdvCSMessage2 extends Message{
	public short sType=peer.tools.MessageType.sType_AdvCSMessage2;
	byte[] dpData;
	String advUrl;
	float rate;
	public AdvCSMessage2(float rate,String advUrl){
		byte[] ad=advUrl.getBytes();
		dpData=new byte[8+ad.length];
		ByteShortSwitch.toByteArray(ver, dpData, 0);  //add: ver
		ByteShortSwitch.toByteArray(sType, dpData, 2);  //add: service type
		short rate2=(short) (rate*100);
		ByteShortSwitch.toByteArray(rate2, dpData, 4);
		short adl=(short) ad.length;
		ByteShortSwitch.toByteArray(adl, dpData, 6);
		System.arraycopy(ad, 0, dpData, 8, ad.length);
		dp=new DatagramPacket(dpData,dpData.length);
		System.out.println("11111111111 "+rate2);
	}
	
	public AdvCSMessage2(DatagramPacket dp){
		this.dp=dp;
		dpData=dp.getData();
		ver=ByteShortSwitch.toShort(dpData, 0);
		sType=ByteShortSwitch.toShort(dpData, 2);
		short rate2=ByteShortSwitch.toShort(dpData, 4);
		rate=(float)rate2/100;
		if(rate>1){
			rate=1;
		}
		short adl=ByteShortSwitch.toShort(dpData, 6);
		byte[] ad=new byte[adl];
		System.arraycopy(dpData, 8, ad, 0, adl);
		advUrl=new String (ad);
		System.out.println("22222222222 "+rate2+" "+rate);
	}
	
	public float getAdvRate(){
		return rate;
	}
	
	public String getAdvUrl(){
		return advUrl;
	}
	
	

}
